import { create } from 'zustand';
import { devtools } from 'zustand/middleware';

// 游戏状态类型定义
export interface GameState {
  // 游戏核心状态
  isGameRunning: boolean;
  isPaused: boolean;
  currentLevel: number;
  gameTime: number;
  score: number;
  
  // 角色状态
  playerHealth: number;
  playerMaxHealth: number;
  playerShield: number;
  playerMaxShield: number;
  playerClass: string;
  playerLevel: number;
  playerExperience: number;
  playerExperienceToNextLevel: number;
  
  // 游戏控制方法
  startGame: () => void;
  pauseGame: () => void;
  resumeGame: () => void;
  endGame: () => void;
  resetGame: () => void;
  
  // 角色相关方法
  takeDamage: (amount: number) => void;
  gainExperience: (amount: number) => void;
  levelUp: () => void;
  
  // 游戏进度更新
  updateGameTime: (delta: number) => void;
  updateScore: (points: number) => void;
}

// 创建游戏状态存储
export const useGameStore = create<GameState>(
  devtools(
    (set) => ({
      // 初始游戏状态
      isGameRunning: false,
      isPaused: false,
      currentLevel: 1,
      gameTime: 0,
      score: 0,
      
      // 初始角色状态
      playerHealth: 100,
      playerMaxHealth: 100,
      playerShield: 0,
      playerMaxShield: 50,
      playerClass: 'gunner', // 默认职业：枪手
      playerLevel: 1,
      playerExperience: 0,
      playerExperienceToNextLevel: 100,
      
      // 游戏控制方法实现
      startGame: () => set({ isGameRunning: true, isPaused: false }),
      pauseGame: () => set({ isPaused: true }),
      resumeGame: () => set({ isPaused: false }),
      endGame: () => set({ isGameRunning: false }),
      resetGame: () => set({
        isGameRunning: false,
        isPaused: false,
        currentLevel: 1,
        gameTime: 0,
        score: 0,
        playerHealth: 100,
        playerMaxHealth: 100,
        playerShield: 0,
        playerLevel: 1,
        playerExperience: 0,
        playerExperienceToNextLevel: 100,
      }),
      
      // 角色相关方法实现
      takeDamage: (amount) => set((state) => {
        // 优先扣除护盾
        let remainingDamage = amount;
        let newShield = state.playerShield;
        
        if (newShield > 0) {
          if (newShield >= remainingDamage) {
            newShield -= remainingDamage;
            remainingDamage = 0;
          } else {
            remainingDamage -= newShield;
            newShield = 0;
          }
        }
        
        // 计算剩余伤害对生命值的影响
        const newHealth = Math.max(0, state.playerHealth - remainingDamage);
        
        return {
          playerShield: newShield,
          playerHealth: newHealth,
          // 如果生命值为0，游戏结束
          isGameRunning: newHealth > 0 ? state.isGameRunning : false
        };
      }),
      
      gainExperience: (amount) => set((state) => {
        const newExperience = state.playerExperience + amount;
        
        // 检查是否升级
        if (newExperience >= state.playerExperienceToNextLevel) {
          return {
            playerExperience: newExperience - state.playerExperienceToNextLevel,
            playerLevel: state.playerLevel + 1,
            playerExperienceToNextLevel: Math.floor(state.playerExperienceToNextLevel * 1.2), // 每级所需经验增加20%
            playerMaxHealth: Math.floor(state.playerMaxHealth * 1.1), // 升级时增加10%最大生命值
            playerHealth: Math.floor(state.playerMaxHealth * 1.1), // 升级时恢复满血
            playerMaxShield: Math.floor(state.playerMaxShield * 1.1), // 升级时增加10%最大护盾值
          };
        }
        
        return { playerExperience: newExperience };
      }),
      
      levelUp: () => set((state) => ({
        playerLevel: state.playerLevel + 1,
        playerExperienceToNextLevel: Math.floor(state.playerExperienceToNextLevel * 1.2),
        playerMaxHealth: Math.floor(state.playerMaxHealth * 1.1),
        playerHealth: Math.floor(state.playerMaxHealth * 1.1),
        playerMaxShield: Math.floor(state.playerMaxShield * 1.1),
      })),
      
      // 游戏进度更新方法
      updateGameTime: (delta) => set((state) => ({
        gameTime: state.gameTime + delta
      })),
      
      updateScore: (points) => set((state) => ({
        score: state.score + points
      })),
    }),
    { name: 'game-store' }
  )
);